<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	
	<style type="text/css">
	    .errorText
	    {
	        color:red;
	    }
    </style>
</head>
<body>
	<form id="form1" method="post">
        <div style="padding-left:30px;padding-bottom:5px;padding-top: 25px;">
            <table style="table-layout:fixed;" cellspacing="0" cellpadding="10">
            	<tr>
            		<td style="width:80px;">编号：</td>
                    <td>    
                        <input name="serialNum" onblur="judgeSerialNum();" errorMode="none" onvalidation="onserialNumValidation" requiredErrorText="编号不能为空" vtype="maxLength:50" class="mini-textbox" required="true" style="width: 180px" emptyText="请输入唯一编号（字母+数字）"/>
                    </td>
                    <td id="serialNum_error" class="errorText" ></td>
            	</tr>
            	<tr>
           			<td>标题：</td>
                    <td>    
                        <input name="title" errorMode="none" onvalidation="checkValidation"  vtype="maxLength:255" class="mini-textbox"  style="width: 180px"/>
                    </td>
                    <td id="title_error" class="errorText" ></td>
            	</tr>
                <tr>
                    <td>仓库</td>
                    <td>
                        <input name="warehouseId" errorMode="none" onvalidation="checkValidation" requiredErrorText="请选择仓库"  class="mini-combobox" required="true" valueField="id" style="width: 180px" textField="name" url="storage_find_warehouse.do" />
                    </td>
                     <td id="warehouseId_error" class="errorText" ></td>
               </tr>
                <tr>
                	<td>入库类型：</td>
                	<td>    
                		<input id="storage_type" name="type"  errorMode="none" onvalidation="checkValidation" requiredErrorText="请选择入库类型"  class="mini-combobox" required="true" valueField="keyName" style="width: 180px" textField="keyValue" />
                	</td>
                	<td id="type_error" class="errorText" ></td>
                </tr>
               <tr>
                    <td>入库时间：</td>
                    <td>
                   		<input name="storageTime"  errorMode="none" onvalidation="checkValidation" requiredErrorText="请选择入库时间"  class="mini-datepicker" required="true" style="width: 180px" emptyText="请选择日期" allowInput="false"/>
                	</td>
                	<td id="storageTime_error" class="errorText" ></td>
                </tr>
            </table>
        </div>
        <div style="text-align:center;padding:10px;">               
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:50px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div>        
    </form>
</body>
</html>

<script type="text/javascript">
	mini.parse();
	
	//加载入库类型数据
	var type = mini.get("storage_type");
	type.load('dictionary_wm_key_val_view.do?typeName=STORAGE_TYPE');
	
	var form = new mini.Form("form1");
	
	//保存数据
	function SaveData() {
	    var o = form.getData(true); 
	    form.validate();
	    if (form.isValid() == false || judgeSerialNum() == false) return;
	    $.ajax({
	        url: "storage_save.do",
			type: 'post',
			dataType: 'json',
	        data: o,
	        cache: false,
	        success: function (text) {
	            if(text.success){
	            	window.CloseOwnerWindow("succ");
	            }else{
	            	mini.alert("数据错误！");
	            }
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            alert(jqXHR.responseText);
	            window.CloseOwnerWindow();
	        }
	    });
	}
	
	//判断编号是否重复
    function judgeSerialNum(){
    	var serialNum = mini.getbyName('serialNum').getValue();
    	var flag = true;
    	if(serialNum.length > 0){
	  	    $.ajax({
	  	    	url: "storage_ver_serialnum.do?serialNum=" + serialNum,
	  	    	dataType: 'json',
	  	        cache: false,
		    	async: false,
	 	        success: function (text) {
	 	            if(text.result){
	 	            	flag = true;
	 	            }else{
	 	            	mini.alert("编码重复请修改（若此列表中不存在，请检查待审核列表。）");
	 	            	flag = false;
	 	            }
	 	        }
	  	    });
    	}
  	    return flag;
    }
	
	

    //标准方法接口定义
    function SetData(data) {
        if (data.action == "edit") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            data = mini.clone(data);
            form.setData(data.row);
            form.setChanged(false);
        }
    }

    function GetData() {
        var o = form.getData();
        return o;
    }
    
    function CloseWindow(action) {            
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
    
    function onOk(e) {
        SaveData();
    }
    
    function onCancel(e) {
        CloseWindow("cancel");
    }
    
    function onserialNumValidation(e) {
    	 var id =e.sender.name + "_error";
         var el = document.getElementById(id);
         checkValidation(e);
        if (e.isValid) {
            if (isEnglishAndNumber(e.value) == false) {
                e.errorText = "必须输入英文+数字";
                el.innerHTML =e.errorText;
                e.isValid = false;
            }
        }
    }
    
    function isEnglishAndNumber(v) {
        var re = new RegExp("^[0-9a-zA-Z\_]+$");
        if (re.test(v)) return true;
        return false;
    }
    
    function updateError(e) {
        var id =e.sender.name + "_error";
        var el = document.getElementById(id);
        if (el) {
            el.innerHTML =e.errorText;
        }
    }
    
    function checkValidation(e) {                  
        updateError(e);
    }
</script>